package com.example.qa.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.qa.commonutils.JwtUtils;
import com.example.qa.commonutils.MD5;
import com.example.qa.commonutils.R;
import com.example.qa.entity.ZhUser;
import com.example.qa.service.ZhUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private ZhUserService userService;
    @PostMapping("/login")
    public R login(@RequestBody ZhUser user){
        String userName =user.getUserName();
        String password =user.getPassword();
        if(StringUtils.isEmpty(userName)||StringUtils.isEmpty(password))
        {
            return R.error();
        }
        QueryWrapper<ZhUser>wrapper=new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        ZhUser one = userService.getOne(wrapper);
        if(one==null)
        {
            return R.error();
        }
        if(one.getStatus()==(short)2)
        {
            return R.error().message("未激活");
        }
        if(!MD5.encrypt(password).equals(one.getPassword()))
        {
            return R.error();
        }
        String token = JwtUtils.getJwtToken(one.getUserName());

        return R.ok().data("token",token);
    }
    @PostMapping("/adminlogin")
    public R adminLogin(){
        return R.ok().data("token","admin");
    }
    @PostMapping("/register")
    public R registerUser(@RequestBody ZhUser user)
    {

        String userName =user.getUserName();
        String password =user.getPassword();
        if(StringUtils.isEmpty(userName)||StringUtils.isEmpty(password))
        {
            return R.error();
        }
        QueryWrapper<ZhUser>wrapper=new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        int count = userService.count(wrapper);
        if(count!=0)
        {
            return R.error();
        }
        user.setStatus((short) 2);
        user.setPassword(MD5.encrypt(password));
        user.setAvatar("2021/02/25/21ac9ddddb1a487c963e1f9b688094522.jpg");
        userService.save(user);
        return R.ok();
    }
    @GetMapping("/info")
    public R info(@RequestParam("token")String jwtToken){
        List<String>roles=new ArrayList<>();
        if(jwtToken.equals("admin")) {
            roles.add("admin");
            return R.ok().data("roles", roles)
                    .data("name", "客服1号")
                    .data("avatar", "2021/02/25/c0c8b4c9d9364352b3eb7f3fa0e7cce24.jpg");
        }

        String userName = JwtUtils.getUserNameByJwtToken(jwtToken);
        QueryWrapper<ZhUser> wrapper=new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        ZhUser user = userService.getOne(wrapper);

        roles.add("user");
        return R.ok().data("roles",roles)
                .data("name",userName)
                .data("avatar",user.getAvatar());
    }
    @PostMapping("/logout")
    public R logout(){
        return R.ok();
    }
}
